package com.wclass.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseAppUser;
import com.wclass.entity.BaseOrgan;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.IBaseOrganService;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 机构 后台控制器
 * </p>
 *
 * @author Bruce
 * @since 2018-12-28
 */
@RestController
@RequestMapping("/sysOrgan")
public class SysOrganController {

    @Autowired
    private IBaseOrganService iBaseOrganService;
    @Autowired
    private IBaseAppUserService iBaseAppUserService;


    /**
     * 机构列表
     * @param limit
     * @param offset
     * @param auditStatus
     * @param name
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/getOrganList", method = RequestMethod.GET)
    public ReturnObject<List<BaseOrgan>> verify(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("审核状态") Integer auditStatus,
            @ApiParam("机构名字") String name) {
        try {
            EntityWrapper<BaseOrgan> ew = new EntityWrapper<>();
            if (auditStatus != null) {
                ew.eq("audit_status", auditStatus);
            }
            if (StringUtils.isNotBlank(name)) {
                ew.like("name", name);
            }
            ew.orderBy("crt_time", false);
            Page<BaseOrgan> obj = iBaseOrganService.selectPage(new Page<>(offset, limit), ew);
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }

    /**
     * 对机构进行审核
     *
     * @param auditStatus
     * @param auditOpinion
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
    public ReturnObject updateStatus(@ApiParam("审核状态") @RequestParam String auditStatus,
                                     @ApiParam("审核意见") @RequestParam String auditOpinion,
                                     @ApiParam("机构id") @RequestParam String id) {
        try {
            BaseOrgan baseOrgan = iBaseOrganService.selectById(id);
            if (baseOrgan != null) {
                baseOrgan.setAuditStatus(Integer.valueOf(auditStatus));
                baseOrgan.setAuditTime(new Date());
                baseOrgan.setAuditOpinion(auditOpinion);
                iBaseOrganService.updateById(baseOrgan);

                return new ReturnObject(0, "审核成功");
            } else {

                return new ReturnObject(1, "审核失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }


    @IgnoreAuth
    @RequestMapping(value = "/updateDisabled", method = RequestMethod.GET)
    public ReturnObject updateState(@ApiParam("禁用状态") @RequestParam Integer disabled,
                                    @ApiParam("机构id") @RequestParam Long id) {
        try {
            BaseOrgan baseOrgan = iBaseOrganService.selectById(id);
            if (baseOrgan != null) {
                //先禁用机构，然后再禁用机构里面的所有老师
                baseOrgan.setDisabled(disabled);
                iBaseOrganService.updateById(baseOrgan);
                List<BaseAppUser> baseAppUsers = iBaseAppUserService.selectList(new EntityWrapper<BaseAppUser>().eq(
                        "institution_id", id));
                if (baseAppUsers != null && baseAppUsers.size() > 0) {

                    for (int i = 0; i < baseAppUsers.size(); i++) {
                        BaseAppUser user=baseAppUsers.get(i);
                        user.setDisabled(disabled);
                        iBaseAppUserService.updateById(user);
                    }
                }
                return new ReturnObject(0, "状态设置成功");
            } else {

                return new ReturnObject(1, "状态设置失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }


}

